package com.zjdiepu.www.biz.cw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.cw.CwRemitAdjustAMapper;
import com.zjdiepu.www.ibiz.cw.ICwRemitAdjustAService;
import com.zjdiepu.www.model.cw.dto.CwRemitAdjustA;
import com.zjdiepu.www.model.cw.ext.CwRemitAdjustAExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年08月02日 15时40分<br/>
 * @since 1.0<br/>
 */
@Service
public class CwRemitAdjustAServiceImpl extends BaseService implements ICwRemitAdjustAService {
	public static final Logger logger = LoggerFactory.getLogger(CwRemitAdjustAServiceImpl.class);

	@Autowired
	private CwRemitAdjustAMapper cwRemitAdjustAMapper;
	
	@Override
	public void saveUpdateAndDelete(CwRemitAdjustA cwRemitAdjustA, OperateType operType) {
		switch (operType) {
		case add:
			cwRemitAdjustA.initInsertData(cwRemitAdjustA.getModifier(), cwRemitAdjustA.getModifierName());
			cwRemitAdjustAMapper.insert(cwRemitAdjustA);
			break;
		case edit:
			cwRemitAdjustA.initUpdateData(cwRemitAdjustA.getModifier(), cwRemitAdjustA.getModifierName());
			// 影响的记录数
			int updateCount = cwRemitAdjustAMapper.updateByPrimaryKeySelective(cwRemitAdjustA);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			cwRemitAdjustA.initDeleteData(cwRemitAdjustA.getModifier(), cwRemitAdjustA.getModifierName());
			// 影响的记录数
			int deleteCount = cwRemitAdjustAMapper.updateByPrimaryKeySelective(cwRemitAdjustA);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<CwRemitAdjustA> selectByExt(CwRemitAdjustAExt cwRemitAdjustAExt) {
		return cwRemitAdjustAMapper.selectByExt(cwRemitAdjustAExt);
	}
	
	@Override
	public int countByExt(CwRemitAdjustAExt cwRemitAdjustAExt){
		return cwRemitAdjustAMapper.countByExt(cwRemitAdjustAExt);
	}
	
	@Override
	public CwRemitAdjustA selectById(String id) {
		return cwRemitAdjustAMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			cwRemitAdjustAMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(CwRemitAdjustA cwRemitAdjustA) {
		return cwRemitAdjustAMapper.countRefined(cwRemitAdjustA);
	}

	@Override
	public CwRemitAdjustA selectRefined(CwRemitAdjustA cwRemitAdjustA) {
		return cwRemitAdjustAMapper.selectRefined(cwRemitAdjustA);
	}

	@Override
	public boolean repeat(CwRemitAdjustA cwRemitAdjustA) {
		int count = countRefined(cwRemitAdjustA);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(cwRemitAdjustA.getId())) {
			return true;
		}
		CwRemitAdjustA dbItem = selectRefined(cwRemitAdjustA);
		if(null == dbItem) {
			return false;
		}
		return !cwRemitAdjustA.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return cwRemitAdjustAMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
